﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Entity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
           Response.ContentType = "text/plain";
    }

    public string TableName
    {
        get
        {
            return Request["TableName"];
        }
    }

    public string ClassName
    {
        get
        {
            return TableName.Substring(0, 1).ToUpper() + TableName.Substring(1);

        }
    }

    public List<Code.DAL.Table> CurrentDables
    {
        get
        {
            return (List<Code.DAL.Table>)Session["Tables"];
        }
    }

    public Code.DAL.Table CurrentTable
    {
        get
        {
            return CurrentDables.Find(x => x.Name == TableName);
        }
    }

    public string TableDesc
    {
        get
        {
            return CurrentTable.Desc;
        }
    }

    public string Columns
    {
        get
        {
            var cols = CurrentTable.Columns;
            StringBuilder sb = new StringBuilder();
            string kg = "        ";
            for (int i = 0; i < cols.Count; i++)
            {
                var c = cols[i];
                var name=c.Name;
                name=name.Substring(0,1).ToUpper()+name.Substring(1);
                sb.AppendLine(kg+"/// <summary>");
                sb.AppendLine(kg+"///"+c.Desc);
                sb.AppendLine(kg+"/// </summary>");
                sb.AppendLine(kg + "public virtual  " + ColType.GetColType(c) + " " + name);
                sb.AppendLine(kg+"{");
                sb.AppendLine(kg + "   get;");
                sb.AppendLine(kg + "   set;");
                sb.AppendLine(kg+"}");
                sb.AppendLine();
            }
            return sb.ToString();
        }
    }

    public string EqualsStr
    {
        get
        {
            if (CurrentTable.Columns.Where(x => x.IsKey).Count() > 1)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("        public override bool Equals(Object obj)");
                sb.AppendLine("        {");
                sb.AppendLine("            var c = (" + ClassName + ")obj;");
                sb.AppendLine("            if (c == null)");
                sb.AppendLine("            {");
                sb.AppendLine("               return false;");
                sb.AppendLine("            }");
                string str = "";
                var lst= CurrentTable.Columns.Where(x => x.IsKey).ToList();
                foreach (var c in lst)
                {
                    str += " c." + StringTools.GetName(c.Name) + " == this." + StringTools.GetName(c.Name) + " " + "&&";
                }
                str = str.TrimEnd('&').TrimEnd('&');
                sb.AppendLine("            if (" + str + ")");
                sb.AppendLine("            {");
                sb.AppendLine("                return true;");
                sb.AppendLine("            }");
                sb.AppendLine("            else");
                sb.AppendLine("            {");
                sb.AppendLine("                return false;");
                sb.AppendLine("            }");
                sb.AppendLine("        }");
                return sb.ToString();
            }
            else
            {
                return "";
            }
        }
    }

    public string GetHashCodeStr
    {
        get
        {
                StringBuilder sb = new StringBuilder();
            if (CurrentTable.Columns.Where(x => x.IsKey).Count() > 1)
            {
                sb.AppendLine("        public override int GetHashCode()");
                sb.AppendLine("        {");
                string str = "";
                var lst= CurrentTable.Columns.Where(x => x.IsKey).ToList();
                foreach (var c in lst)
                {
                    str +=StringTools.GetName(c.Name)+ ".GetHashCode() ^ ";
                }
                str = str.TrimEnd(' ').TrimEnd('^');
                sb.AppendLine("             return "+str+";");
                sb.AppendLine("        }");
                return sb.ToString();
            }
            else
            {
                return sb.ToString();
            }
        }
    }

}

     //public override int GetHashCode()
     //   {
     //       return UserName.GetHashCode() ^ Stock.GetHashCode();
     //   }